package com.backend.system.service.impl;

import com.backend.system.dao.entity.RoleUser;
import com.backend.system.dao.mapper.RoleUserMapper;
import com.backend.system.service.IRoleUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户角色关系表 服务实现类
 * </p>
 *
 * @author backend-manage
 * @since 2024-04-02
 */
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements IRoleUserService {

    /**
     * 通过用户ID查角色
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @Override
    public List<RoleUser> getRoleUsersByUserId(Long userId) {
        LambdaQueryWrapper<RoleUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleUser::getUserId, userId);
        queryWrapper.orderByAsc(RoleUser::getId);

        List<RoleUser> roleUsers = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(roleUsers)) {
            return Collections.emptyList();
        }
        return roleUsers;
    }

    /**
     * 通过用户ID删除
     *
     * @param userId 用户ID
     * @return 删除结果
     */
    @Override
    public Boolean removeByUserId(Long userId) {
        LambdaUpdateWrapper<RoleUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleUser::getUserId, userId);
        return this.remove(updateWrapper);
    }

    /**
     * 通过用户ID列表删除
     *
     * @param userIdList 用户ID列表
     * @return 删除结果
     */
    @Override
    public Boolean removeByUserIdList(List<Long> userIdList) {
        LambdaUpdateWrapper<RoleUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(RoleUser::getUserId, userIdList);
        return this.remove(updateWrapper);
    }
}
